package com.dayaokeji.rhythmschoolstudent.client.mine.account;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ActivityStackManager;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.dayaokeji.rhythmschoolstudent.client.mine.account.adapter.SchoolListAdapter;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.SchoolApi;
import com.dayaokeji.server_api.domain.School;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

/**
 * 院校列表
 */
public class DepartmentListActivity extends BaseActivity {

    public static final String EXTRA_PARENT_ID = "parent_id";
    public static final String EXTRA_TITLE = "title";
    public static final String EXTRA_ENTITY = "entity";

    private static final SchoolApi schoolApi = ApiUtils.getApi(SchoolApi.class);
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_faculty_list)
    RecyclerView rvFacultyList;
    private long parentId;

    private List<School> facultyList = new ArrayList<>();
    private Call<ServerResponse<List<School>>> facultyCall;
    private SchoolListAdapter facultyListAdapter;
    private String title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_faculty_list);
        setSupportActionBar(toolbar);
        parentId = getIntent().getLongExtra(EXTRA_PARENT_ID, -1);
        title = getIntent().getStringExtra(EXTRA_TITLE);

        if (parentId == -1 || TextUtils.isEmpty(title)) {
            ToastUtils.warning(R.string.data_incorrect);
            return;
        }

        init();
    }

    private void init() {
        getSupportActionBar().setTitle(title);
        setRecyclerView();
        setOnListener();
        getFacultyData();
    }


    private void setRecyclerView() {
        rvFacultyList.setLayoutManager(new LinearLayoutManager(this));
        rvFacultyList.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .marginResId(R.dimen.default_divider_height)
                .build());
        if (facultyListAdapter == null) {
            facultyListAdapter = new SchoolListAdapter(facultyList);
        }
        rvFacultyList.setAdapter(facultyListAdapter);
        facultyListAdapter.setEmptyView(R.layout.empty_layout,rvFacultyList);
    }


    private void setOnListener() {
        facultyListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                School school = (School) adapter.getData().get(position);
                Intent intent = getIntent();
                intent.putExtra(EXTRA_ENTITY,school);
                setResult(RESULT_OK,intent);
                ActivityStackManager.finish();
            }
        });
    }

    /**
     * 获取院校数据
     */
    private void getFacultyData() {
        if (parentId != -1) {
            facultyCall = schoolApi.department(parentId);
            facultyCall.enqueue(new ServerCallback<List<School>>(this,"正在查询...") {
                @Override
                public void onResponse(boolean success, ServerResponse<List<School>> response) {
                    if (success) {
                        facultyList.clear();
                        facultyList.addAll(response.getBody());
                        facultyListAdapter.notifyDataSetChanged();
                    }else{
                        ToastUtils.error(getString(R.string.search_data_failure));
                    }
                }
            });
        }else{
            ToastUtils.error(getString(R.string.data_incorrect));
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (facultyCall != null) {
            facultyCall.cancel();
        }
    }
}
